#include <iostream>
#include <vector>
#include "Thread.hpp"
#include "ringqueue.hpp"
#include <string>

using std::cout;
using std::endl;
using namespace ThreadModule;

using ringqueue_t = RingQueue<int>;

void Consumer(ringqueue_t *&rq)
{

    while (true)
    {
        sleep(1);
        cout << "consumer .... " << endl;
    }
}

void Productor(ringqueue_t *&rq)
{

    while (true)
    {
        sleep(1);

        cout << "product .... " << endl;
    }
}

void StartCom(std::vector<Thread<ringqueue_t *>> *&threads, int num, ringqueue_t *&bq, func_t<ringqueue_t *> func)
{
    for (int i = 0; i < num; i++)
    {
        std::string name = "thread -" + std::to_string(1 + i);
        threads->emplace_back(func, bq, name);
        threads->back().Start();
    }
}

void StartConsumer(std::vector<Thread<ringqueue_t *>> *&threads, int num, ringqueue_t *&bq)
{
    StartCom(threads, num, bq, Consumer);
}

void StartProductor(std::vector<Thread<ringqueue_t *>> *&threads, int num, ringqueue_t *&bq)
{
    StartCom(threads, num, bq, Productor);
}

void WaitAllThread(std::vector<Thread<ringqueue_t *>> &threads)
{
    for (auto &thread : threads)
    {
        thread.Join();
    }
}

int main()
{
    ringqueue_t *rq = new ringqueue_t; // 全局定义一个配合ThreadData

    std::vector<Thread<ringqueue_t *>> threads;
    std::vector<Thread<ringqueue_t *>> *pthreads = &threads;

    StartConsumer(pthreads, 5, rq);
    StartProductor(pthreads, 1, rq);
    WaitAllThread(threads);

    return 0;
}